package com.webSite.demo.cy.downBook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;


public class ReadWord {

	/**
	 * 读取word文件内容(只能读取word2003)
	 * 
	 * @param path
	 * @return buffer
	 */
 
	public static String readWord(String path) {
		String buffer = "";
		try {
			if (path.endsWith(".doc")) {
				try {
					InputStream is = new FileInputStream(new File(path));
					WordExtractor ex = new WordExtractor(is);
					buffer = ex.getText();
					ex.close();
				}catch(IllegalArgumentException ex1) {
					OPCPackage opcPackage = POIXMLDocument.openPackage(path);
					POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
					buffer = extractor.getText();
					extractor.close();
				}
			} else if (path.endsWith("docx")) {
				OPCPackage opcPackage = POIXMLDocument.openPackage(path);
				POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
				buffer = extractor.getText();
				extractor.close();
			} else {
				System.out.println("此文件不是word文件！");
			}
 
		} catch (Exception e) {
			e.printStackTrace();
		}
 
		return buffer;
	}
 
	public static void main(String[] args) {
		String content = readWord("C:\\Users\\yanfa00027\\Desktop\\识别内容\\1.doc");
		System.out.println("content===="+content);
	}

}
